package fr.umlv.m2.recipes.ideas;

import java.util.List;

import fr.umlv.m2.recipes.ideas.bean.IngredientBean;
import fr.umlv.m2.recipes.ideas.bean.RecipeBean;
import fr.umlv.m2.recipes.ideas.sqlite.db.objects.RecipesDB;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

/**
 * Activity use to show the Ingredients
 * @author Younes Chandarli et Christophe Chillan
 *
 */
public class IngredientView extends Activity{
	private TextView ingredientTextView;
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView textview = new TextView(this);
	    textview.setText("Vous dans la rubrique ingredient");
	    setContentView(R.layout.ingredientview);
	    
	    Bundle extras = getIntent().getExtras(); 
	    if(extras!=null){
	    	final RecipeBean recipe = (RecipeBean) getIntent().getExtras().getSerializable("recipe");
	    	ingredientTextView = (TextView) findViewById(R.id.ingredients);
	    	List<IngredientBean> ingredients = RecipesDB.getIngredientDao().getIngredientFromRecipeId(RecipesDB.getDatabase(), recipe.getId());
	    	StringBuilder builder = new StringBuilder();
	    	for(IngredientBean ingredient : ingredients){
	    		builder.append("- ").append(ingredient.getName()).append("\n");
	    	}
	    	ingredientTextView.setText(builder.toString());
	    }
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	/**
	 * Use to Define the Option Menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		// On regarde quel item a été cliqué grâce à son id et on déclenche une
		// action
		switch (item.getItemId()) {
			case R.id.home:
				startActivity(new Intent(this, MenuTabWidget.class));
				return true;
			case R.id.about:
				startActivity(new Intent(this, AboutView.class));
				return true;
			default:
		        return super.onOptionsItemSelected(item);
			}
		
	}

}
